<?php
/**
* @version $Id: search.php 10002 2008-02-08 10:56:57Z willebil $
* @package Joomla
* @subpackage Search
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

require_once( $mainframe->getPath( 'front_html' ) );

$frame		= mosGetParam($_REQUEST, 'frame', '');

// page title
$mainframe->setPageTitle( _SEARCH_TITLE );

switch ( $task ) {
	case 'search':
		showSearch();
		break;
	case 'frame':
		showFrame($frame);
		break;		
	default:
		showSearch();
		break;
}

function showSearch (){
	global $mosConfig_list_limit, $database, $mosConfig_live_site, $mainframe;
	$limit	= intval( mosGetParam( $_GET, 'limit', $mosConfig_list_limit ) );
	$limit	= ( $limit ? $limit : $mosConfig_list_limit );
	$limitstart = intval( mosGetParam( $_GET, 'limitstart', 0 ) );
	
	$searchword			= mosGetParam($_REQUEST, 'searchword', '');
	$change				= str_replace(",", " ", $searchword);
	$change				= str_replace("-", " ", $change);
	$key 				= explode(" ",$change);
	
	$condition 	= "";
	$pageLink 	= "";
	
	$numberPage = 10;
	
	if ($searchword){
		$condition .= " AND title LIKE'%".$searchword."%'";
		$pageLink .= "&searchword=".$searchword;
		$dk = " AND (title LIKE'%".$searchword."%'";
		for ($i=0; $i<count($key); $i++){
			if ($key[$i]!=""){
				$dk .= " OR title LIKE'%".$key[$i]."%'";
			}
		}
		$dk .=")";
		$condition .= $dk;
	}
	
	$sql = "SELECT COUNT(*) FROM #__content WHERE state=1".$condition;
	$database->setQuery($sql);
	$total = $database->loadResult();
	$sql = "SELECT * FROM #__content WHERE state=1".$condition." ORDER BY sectionid";
	$database->setQuery( $sql, $limitstart, $numberPage );
	$rows = $database->loadObjectlist();
	
	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $numberPage );
	$link_page = sefRelToAbs($mosConfig_live_site."/index.php?option=com_search&task=search".$pageLink);
	
	search_html::displaySearch( $total, $rows, $pageNav, $link_page, $numberPage, $searchword );
}

function showFrame( $frame ){
	global $database, $mosConfig_live_site, $mainframe;
	$limit	= intval( mosGetParam( $_GET, 'limit', $mosConfig_list_limit ) );
	$limit	= ( $limit ? $limit : $mosConfig_list_limit );
	$limitstart = intval( mosGetParam( $_GET, 'limitstart', 0 ) );
	
	$numberPage = 30;
	
	$condition = " AND sectionid=1";
	if ($frame=='D300k'){
		$condition .= " AND ((price<300000 AND price>0) OR (price_giam<300000 AND price_giam>0))";
	}else if ($frame=='300k_500k'){
		$condition .= " AND ((price<500000 AND price>=300000) OR (price_giam<500000 AND price_giam>=300000))";
	}else if ($frame=='500k_1MB'){
		$condition .= " AND ((price<1000000 AND price>=500000) OR (price_giam<1000000 AND price_giam>=500000))";
	}else if ($frame=='1MB_3MB'){
		$condition .= " AND ((price<3000000 AND price>=1000000) OR (price_giam<3000000 AND price_giam>=1000000))";
	}else if ($frame=='3MB_5MB'){
		$condition .= " AND ((price<5000000 AND price>=3000000) OR (price_giam<5000000 AND price_giam>=3000000))";
	}else if ($frame=='5MB_10MB'){
		$condition .= " AND ((price<10000000 AND price>=5000000) OR (price_giam<10000000 AND price_giam>=5000000))";
	}else{
		$condition .= " AND (price>10000000 OR price_giam>10000000)";
	}
	
	$sql = "SELECT COUNT(*) FROM #__content WHERE state=1".$condition;
	$database->setQuery($sql);
	$total = $database->loadResult();	
	$sql = "SELECT * FROM #__content WHERE state=1".$condition." ORDER BY ordering ASC, id DESC";
	$database->setQuery( $sql, $limitstart, $numberPage );
	$rows = $database->loadObjectlist();
	
	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, 30 );
	$link_page = $mosConfig_live_site."/index.php?option=com_search&task=frame&frame=".$frame;
	
	search_html::displaySearchProduct( $total, $rows, $pageNav, $link_page, $numberPage );
}
?>